package com.shuangxianglinks;



public class shuangxiang {
    public static void main(String[] args) {
        A a1 = new A(1,22);
        A a2 = new A(2,33);
        A a3 = new A(3,44);
        A a4 = new A(4,55);
        A a5 = new A(5,66);
        A a6 = new A(3,222);
        A a7 = new A(2,34432);
        link link1 = new link();
        link1.add(a1);
        link1.add(a2);

        link1.add(a4);
        link1.add(a5);
        link1.add(a3);
        link1.bl();
        link1.find(2);
        link1.gb(a6);
        System.out.println("-------");
        link1.bl();
        link1.del(a2);
        System.out.println("--------");
        link1.bl();
        System.out.println("--------");
        link1.add(a7);
       link1.bl();
    }
}
class link {
    A head = new A(0,0);

    public void add(A a) {
        A temp = head;
       boolean flag=true;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if ( temp.next.n > a.n) {
                flag=false;
                break;
            }

           temp = temp.next;
        }
        if(flag) {
            temp.next = a;
            a.bear = temp;
        }
        else{
            temp.next.bear=a;
            a.next=temp.next;
            temp.next=a;
            a.bear=temp;
        }
    }

    public void del(A a) {
        A temp = head.next;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = true;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.n == a.n) {
                flag = false;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("你要找的值不存在");
        } else {
            temp.bear.next = temp.next;
            if (temp.next!= null) {
                temp.next.bear = temp.bear;
            }
        }
    }

    public void gb(A a) {
        A temp = head.next;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = true;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.n == a.n) {
                flag = false;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.println("你要添加的值不存在");
        } else {
            temp.n = a.n;
            temp.s=a.s;
        }
    }

    public void find(int a) {
        A temp = head.next;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = true;
        int m = 1;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.n == a) {
                flag = false;
                break;
            }
            temp = temp.next;
            m++;
        }
        if (flag) {
            System.out.println("你要找的值不存在");
        } else {
            System.out.println("你要找的值在" + m + "位");
            System.out.println(temp.n+"\t"+temp.s);
        }
    }

    public void bl() {
        A temp = head.next;
        if (temp == null) {
            System.out.println("链表为空");
            return;
        }
        boolean flag = true;
        int m = 0;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println("\t" + temp.n+"\t"+temp.s);
            temp = temp.next;
        }
    }
}

 class A{
  public int n;
  public int s;
  public  A next;
  public  A bear;

    public A(int n,int s) {
        this.n = n;
        this.s=s;

    }
}